//
//  GaugeView.swift
//  Duyiguqin
//
//  Created by 张昭 on 2024/8/4.
//

import UIKit

import UIKit

class GaugeView: UIView {

    // MARK: - Properties
    private let range: CGFloat = 180 // 范围从 -150 到 -30
    private let startAngle: CGFloat = -150 * .pi / 180 // -150 度
    private let endAngle: CGFloat = -30 * .pi / 180 // -30 度
    private let highlightStartAngle: CGFloat = -100 * .pi / 180 // -100 度
    private let highlightEndAngle: CGFloat = -80 * .pi / 180 // -80 度

    private var gaugeLayer: CAShapeLayer!
    private var needleLayer: CAShapeLayer!
    private var arcLayer: CAShapeLayer!
    
    private let needleLength: CGFloat = 100
    private let needleWidth: CGFloat = 2
    
    private let innerRadius: CGFloat = 50
    private let outerRadius: CGFloat = 100
    private let lineWidth: CGFloat = 30

    private var value: CGFloat = 0 {
        didSet {
            updateNeedle()
        }
    }
    
    // MARK: - Initializers
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupView()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupView()
    }
    
    // MARK: - Setup
    private func setupView() {
        
        arcLayer = CAShapeLayer()
        arcLayer.fillColor = UIColor.clear.cgColor
        arcLayer.strokeColor = UIColor(displayP3Red: 232/255.0, green: 225/255.0, blue: 202/255.0, alpha: 1).cgColor
        arcLayer.lineWidth = lineWidth
        layer.addSublayer(arcLayer)

        drawArc()
        
    }
    
    private func drawArc() {
        let path = UIBezierPath()
        let center = CGPoint(x: bounds.midX, y: bounds.midY)
        let radius: CGFloat = 150
        
        path.addArc(withCenter: center, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: true)

        
        arcLayer.path = path.cgPath
        
//        addTicks()
    }
    
    private func addTicks() {
        let tickLayer = CAShapeLayer()
        let tickPath = UIBezierPath()
        let center = CGPoint(x: bounds.midX, y: bounds.midY)
        let radius = min(bounds.width, bounds.height) / 2
        
        for angle in stride(from: startAngle, through: endAngle, by: .pi / 9) { // Every 20 degrees
            let startX = center.x + radius * cos(angle)
            let startY = center.y + radius * sin(angle)
            let endX = center.x + (radius + 10) * cos(angle)
            let endY = center.y + (radius + 10) * sin(angle)
            
            tickPath.move(to: CGPoint(x: startX, y: startY))
            tickPath.addLine(to: CGPoint(x: endX, y: endY))
        }
        
        tickLayer.path = tickPath.cgPath
        tickLayer.strokeColor = UIColor.black.cgColor
        tickLayer.lineWidth = 1
        layer.addSublayer(tickLayer)
    }
    
    private func drawNeedle() {
        let path = UIBezierPath()
        let center = CGPoint(x: bounds.midX, y: bounds.midY)
        let endPoint = CGPoint(x: center.x + needleLength * cos(startAngle), y: center.y + needleLength * sin(startAngle))
        
        path.move(to: center)
        path.addLine(to: endPoint)
        
        needleLayer.path = path.cgPath
    }
    
    // MARK: - Update Methods
    private func updateNeedle() {
        return
        let angle = (value / range) * (endAngle - startAngle) + startAngle
        let animation = CABasicAnimation(keyPath: "transform.rotation.z")
        animation.fromValue = needleLayer.presentation()?.value(forKeyPath: "transform.rotation.z") ?? 0
        animation.toValue = angle
        animation.duration = 0.5
        animation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
        
        needleLayer.add(animation, forKey: "rotation")
        needleLayer.setValue(angle, forKeyPath: "transform.rotation.z")
    }
    
    // MARK: - Public Methods
    func setValue(_ newValue: CGFloat) {
        value = max(min(newValue, range), -range) // Clip value to the range
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        drawArc()
//        drawNeedle()
    }
}
